package com.sxsihe.heartmate.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView   {
    private Context mContext;
    private float mX;
    private float mY;
    private SurfaceHolder sfh;
    private Canvas canvas;
    private float mCurveEndX;
    private float mCurveEndY;
    private final Paint mGesturePaint = new Paint();
    private final Path mPath = new Path();
    private final Rect mInvalidRect = new Rect();
    private boolean isDrawing;

    public MySurfaceView(Context context) {
        super(context);
        mContext = context;
        sfh = this.getHolder();
        mGesturePaint.setAntiAlias(true);
        mGesturePaint.setStyle(Paint.Style.STROKE);
        mGesturePaint.setStrokeWidth(5);
        mGesturePaint.setColor(Color.WHITE);
    }

    public void drawCanvas(){
        canvas = sfh.lockCanvas();
        if(canvas!=null){
            canvas.drawColor(Color.BLACK);
            canvas.drawPath(mPath,mGesturePaint);
            sfh.unlockCanvasAndPost(canvas);
        }
    }

    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                touchDown(event);
                invalidate();
                return true;
            case MotionEvent.ACTION_MOVE:
                if(isDrawing){
                    Rect rect = touchMove(event);
                    if(rect!=null){
                        invalidate(rect);
                    }
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                if(isDrawing){
                    touchUp(event);
                    invalidate();
                    return true;
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    private void touchDown(MotionEvent event){
        isDrawing = true;
        mPath.reset();
        float x = event.getX();
        float y = event.getY();
        mX = x ;
        mY = y;
        mPath.moveTo(x,y);
        mInvalidRect.set((int) x, (int) y,(int)x,(int)y);
        mCurveEndX = x;
        mCurveEndY = y;
    }

    private Rect touchMove(MotionEvent event){
        Rect areaToRefresh = null;
        float x = event.getX();
        float y = event.getY();
        float previousX = mX;
        float previousY = mY;
        float dx = Math.abs(x-previousX);
        float dy = Math.abs(y-previousY);
        areaToRefresh = mInvalidRect;
        areaToRefresh.set((int)previousX,(int)previousY,(int)x,(int)y);
        float cX = mCurveEndX = (x+previousX)/2;
        float cY = mCurveEndY = (x+previousY)/2;
        mPath.quadTo(previousX, previousY, cX, cY);
//        areaToRefresh.union((int) previousX, (int) previousY, (int) previousX, (int) previousY);
//        areaToRefresh.union((int)cX,(int)cY,(int)cX,(int)cY);
        mX = x;
        mY = y;
        drawCanvas();
        return areaToRefresh;
    }

    void touchUp(MotionEvent event){
        isDrawing = false;
    }
}
